//
//  BatchWeightView.m
//  pa
//
//  Created by HaiJiao Chen on 12-8-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BatchWeightView.h"
#import "TicketViewBG.h"
#import "KXUIColor.h"

@interface BatchWeightBG : ListControllerBG
@end


@implementation BatchWeightView
@synthesize table;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        batchWeight = [[BatchWeight alloc] init];
        batchWeightDatasource = [[BatchWeightDataSource alloc] init]; 
    }
    return self;
}

- (IBAction)cancel:(id)sender
{
    [self removeFromSuperview];
}


-(void)dealloc
{
    [batchWeight release];
    [batchWeightDatasource release];
    [super dealloc];
}
-(void)setData:(NSArray*)_data
{
    [batchWeightDatasource setData:_data];

    [batchWeight setupController:table data:batchWeightDatasource reuse:@"items"];
    BatchWeightBG* bg = [[BatchWeightBG alloc] initWithFrame:CGRectZero];
    [batchWeight setBGWithColor1:[UIColor colorWithARGB:0xFFF7F7F7] color2:[UIColor colorWithARGB:0xFFC4CCD6] height:20 bg:bg];
    [bg release];
}
@end


@implementation BatchWeightDataSource
@synthesize data;
-(void)dealloc
{
    [data release];
    [super dealloc];
}
-(void)setData:(NSArray*)_data
{
    [_data retain];
    [data release];
    data = _data;
}
@end

@implementation BatchWeight

-(void)createSubView:(UITableViewCell*)cell
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 138, 20)];
    [cell.contentView addSubview:label];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.tag = 1;
    [label release];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(138*1, 0, 138, 20)];
    [cell.contentView addSubview:label];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.tag = 2;
    [label release];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(138*2, 0, 138, 20)];
    [cell.contentView addSubview:label];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.tag = 3;
    [label release];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(138*3, 0, 138, 20)];
    [cell.contentView addSubview:label];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.tag = 4;
    [label release];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(138*4, 0, 138, 20)];
    [cell.contentView addSubview:label];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.tag = 5;
    [label release];
    cell.contentView.backgroundColor = [UIColor clearColor];
}
-(void)setupNewCell:(UITableViewCell*)cell obj:(id<NSObject>)obj indexPath:(NSIndexPath*)indexPath
{
    NSDictionary* dic = (NSDictionary*)obj;
    UILabel* name = (UILabel*)[cell.contentView viewWithTag:1];
    UILabel* target_weight = (UILabel*)[cell.contentView viewWithTag:2];
    UILabel* actual_weight = (UILabel*)[cell.contentView viewWithTag:3];
    UILabel* unit = (UILabel*)[cell.contentView viewWithTag:4];
    UILabel* moisture = (UILabel*)[cell.contentView viewWithTag:5];
    
    name.text = [dic valueForKey:@"Name"];
    target_weight.text = [dic valueForKey:@"Target"];
    actual_weight.text = [dic valueForKey:@"Actual"];
    unit.text = [dic valueForKey:@"Unit"];
    moisture.text = [dic valueForKey:@"Moisture"];
}
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath obj:(id<NSObject>)obj
{
    return 20;
}

@end


@implementation BatchWeightBG
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef g = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(g);
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    CGContextFillRect(g, CGRectMake(138, 0, 1, self.frame.size.height));
    CGContextFillRect(g, CGRectMake(276, 0, 1, self.frame.size.height));
    CGContextFillRect(g, CGRectMake(414, 0, 1, self.frame.size.height));
    CGContextFillRect(g, CGRectMake(552, 0, 1, self.frame.size.height));
    UIGraphicsPopContext();
}
- (void)dealloc
{
    [super dealloc];
}
@end


